<?php
/**
* Copyright 2009 Waag Society <http://www.waag.org>
* This file is part of Annotrax.
*
* Annotrax is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* Annotrax is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Annotrax.
* If not, see http://www.gnu.org/licenses/gpl.html
*/
/**
 * default data mapper for movie
 */
class Default_Model_MovieMapper extends At_Model_Mapper{
    protected $tableName = 'movie';

	/**
	 * Provides a fetchCount on the current movie object
	 * without fetching all the results.
	 *
	 * @param Default_Model_Movie $movie
	 */
    public function getCount( Default_Model_Movie $movie ){
		$db = $this->getTable()->getAdapter();

		if( null !== $movie->getChannel() ){
			$query = '
				SELECT COUNT(m.id) AS total
				FROM channel_movie cm
				LEFT JOIN movie m ON m.id = cm.movie_id
				WHERE cm.channel_id = ?
			';

			$result = $db->fetchRow( $query, $movie->getChannel()->id );
		}
		else{
			$result = $db->fetchRow('SELECT COUNT(id) AS total FROM movie');
		}

		return $result['total'];
    }

	/**
	 * Fetch all movies for current $movie
	 *  - if channel is set, limit results for channel
	 *  - else fetch all from movie table
	 *  - use movie getLimit and movie Offset for pager results
	 *
	 *  @param Default_Model_Movie $movie
	 */
    public function fetchAll( Default_Model_Movie $movie ){
		$db = $this->getTable()->getAdapter();
		$bind = array();
		$collect = array();

		if( null !== $movie->getChannel() ){
			$query = '
				SELECT m.*
				FROM channel_movie cm
				LEFT JOIN movie m ON m.id = cm.movie_id
				WHERE cm.channel_id = :channel_id
				ORDER BY m.date ASC
			';

			$bind['channel_id'] = $movie->getChannel()->id;
		}
		else{
			$query = '
				SELECT *
				FROM movie
				ORDER BY date ASC
			';
		}

		// @todo find a way to do this the Zend like way
		if( (int) $movie->getLimit() > 0 ){
			$query .= sprintf(' LIMIT %d ', $movie->getLimit() );
		}
		if( (int) $movie->getOffset() > 0 ){
			$query .= sprintf( ' OFFSET %d', $movie->getOffset() );
		}

		$result = $db->fetchAll( $query, $bind, Zend_Db::FETCH_ASSOC );

		foreach( $result as $values ){
			$collect[] = new Default_Model_Movie( $values );
		}

		return $collect;
    }

	/**
	 * Update or insert movie
	 * @param Default_Model_Movie $movie Movie to save
	 */
	public function save( Default_Model_Movie $movie ){
		$values = array(
			'title'			=> $movie->title,
			'poster_frame'	=> $movie->posterFrame,
			'creator'		=> $movie->creator, //LL veld niet in sql scheme
			'subject'		=> $movie->subject,
			'description'	=> $movie->description,
			'publisher'		=> $movie->publisher,
			'contributor'	=> $movie->contributor,
			'date'			=> $movie->date,
			'language'		=> $movie->language,
			'rights'		=> $movie->rights,
			'filename'		=> $movie->filename
		);

		$values = array_filter( $values );//?

        if( null === $movie->id ){
            $movie->id = $this->getTable()->insert( $values );
        }
        else{
//			var_dump( $movie->filename );

	///		var_dump( $values );

            $this->getTable()->update( $values, array( 'id = ?' => $movie->id ));
        }
	}

	/**
	 * Update channels that are enabled for each movie.
	 * @param Default_Model_Movie $movie Movie to save
	 */
	public function setChannels( Default_Model_Movie $movie ){
		$ids = array();
		$db  = $this->getTable()->getAdapter();

		$db->delete('channel_movie', sprintf('movie_id = %d', $movie->id));

		foreach( $movie->getChannels() as $channel ){
			$db->insert('channel_movie',array(
				'channel_id' => $channel->id,
				'movie_id' => $movie->id
			));
		}
	}

	/**
	 * delete a row from movie
	 *
	 * @param Default_Model_Movie $movie to delete
	 * @return void
	 */
	public function delete( Default_Model_Movie $movie ){
		$db = $this->getTable()->getAdapter();
		$db->delete( 'channel_movie', sprintf('movie_id = %d', $movie->id ) );
		$db->delete( 'annotation', sprintf('movie_id = %d', $movie->id ) );
		$db->delete( 'video_info', sprintf('movie_id = %d', $movie->id ) );
		$db->delete( 'frame', sprintf('movie_id = %d', $movie->id ) );

        $this->getTable()->delete(array('id = ?' => $movie->id));
	}
}
